﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace AdobePolicyFileServer
{
    class PolicyContent
    {
        const String POLICY_FILE = "flashpolicy.xml";
        static PolicyContent inst = null;
        private byte[] xmlContent;

        public byte[] XmlContent
        {
            get { return xmlContent; }
        }


        private PolicyContent() {
            char[] chars = null;
            if (File.Exists(POLICY_FILE))
            {
                try
                {
                    xmlContent =  File.ReadAllBytes(POLICY_FILE);
                }
                catch{
                    
                }
                if(chars == null)
                {
                    String defaultXml = 
                    "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"+
                    "<cross-domain-policy xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+ 
                    "xsi:noNamespaceSchemaLocation=\"http://www.adobe.com/xml/schemas/PolicyFileSocket.xsd\">"+
                    "<allow-access-from domain=\"*\" to-ports=\"*\" secure=\"false\" />"+
                    "<site-control permitted-cross-domain-policies=\"all\" />"+
                    "</cross-domain-policy>";
                    chars = defaultXml.ToCharArray();
                }
            }
            else 
            {
                String defaultXml = 
                    "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"+
                    "<cross-domain-policy xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+ 
                    "xsi:noNamespaceSchemaLocation=\"http://www.adobe.com/xml/schemas/PolicyFileSocket.xsd\">"+
                    "<allow-access-from domain=\"*\" to-ports=\"*\" secure=\"false\" />"+
                    "<site-control permitted-cross-domain-policies=\"all\" />"+
                    "</cross-domain-policy>";
                chars = defaultXml.ToCharArray();
            }
            if (chars != null)
            {
                xmlContent= new byte[chars.Length];
                for (int i = 0; i < chars.Length; i++)
                {
                    xmlContent[i] = (byte)chars[i];
                }
            }
        }

        public static PolicyContent Get()
        {
            lock (typeof(PolicyContent))
            {
                if (inst == null)
                    inst = new PolicyContent();
                return inst;
            }
        }


    }
}
